package com.platform.modules.sys.controller;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.platform.common.annotation.SysLog;
import com.platform.common.page.PageUtil;
import com.platform.common.utils.Constant;
import com.platform.common.utils.Query;
import com.platform.common.utils.RestResponse;
import com.platform.common.validator.AbstractAssert;
import com.platform.common.validator.ValidatorUtils;
import com.platform.common.validator.group.AddGroup;
import com.platform.common.validator.group.UpdateGroup;
import com.platform.common.enumstatus.AptitudeEnum;
import com.platform.common.enumstatus.SystemEnum;
import com.platform.modules.quality.vo.CommonResult;
import com.platform.modules.sys.dto.ContactsByAppDto;
import com.platform.modules.sys.entity.SysOrgEntity;
import com.platform.modules.sys.entity.SysOrgUserEntity;
import com.platform.modules.sys.entity.SysRoleEntity;
import com.platform.modules.sys.entity.SysUserAptitudeEntity;
import com.platform.modules.sys.entity.SysUserEntity;
import com.platform.modules.sys.form.PasswordForm;
import com.platform.modules.sys.service.SysDictService;
import com.platform.modules.sys.service.SysOrgService;
import com.platform.modules.sys.service.SysOrgUserService;
import com.platform.modules.sys.service.SysRoleService;
import com.platform.modules.sys.service.SysUserAptitudeService;
import com.platform.modules.sys.service.SysUserRoleService;
import com.platform.modules.sys.service.SysUserService;

/**
 * 系统用户  
 *           
 * @author 肖亮
 */
@RestController
@RequestMapping("/sys/user")
public class SysUserController extends AbstractController {
	@Autowired
	private SysUserService sysUserService;
	@Autowired
	private SysOrgUserService sysOrgUserService;
	@Autowired
	private SysUserRoleService sysUserRoleService;
	@Autowired
	private SysRoleService sysRoleService;
	@Autowired
	private SysOrgService sysOrgService;
	@Autowired
	private SysUserAptitudeService sysUserAptitudeService;
	@Autowired
	private SysDictService sysDictService;
	

	/**
	 * 查看所有列表
	 * @param params
	 * @return RestResponse
	 */
	@ApiOperation(value = "查询用户列表", notes = "查询用户列表")
	@ApiImplicitParams({
	@ApiImplicitParam(paramType = "query", name = "userName", value = "人员姓名", dataType = "String"),
	@ApiImplicitParam(paramType = "query", name = "orgNo", value = "机构编码", dataType = "String")
	})
	@PostMapping("/queryAll")
//	@RequiresPermissions("sys:dict:list")
	public RestResponse queryAll(String userName,String orgNo) {
		Map<String, Object> params = new HashMap<String, Object>();
		List<SysUserEntity> list = sysUserService.queryAll(params);
		return RestResponse.success().put("list", list);
	}

	/**
	 * 所有用户列表
	 * @param params
	 * @return RestResponse
	 */
	@ApiOperation(value = "查询用户列表", notes = "查询用户列表")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType = "query", name = "currPage", value = "当前页数", dataType = "Integer"),
		@ApiImplicitParam(paramType = "query", name = "limit", value = "分页步数", dataType = "Integer"),
		@ApiImplicitParam(paramType = "query", name = "orgName", value = "机构名称", dataType = "String"),
		@ApiImplicitParam(paramType = "query", name = "orgType", value = "机构类型", dataType = "String"),
		@ApiImplicitParam(paramType = "query", name = "roleCode", value = "角色编码", dataType = "String"),
		@ApiImplicitParam(paramType = "query", name = "userName", value = "登录名", dataType = "String"),
		@ApiImplicitParam(paramType = "query", name = "realName", value = "姓名", dataType = "String"),
		@ApiImplicitParam(paramType = "query", name = "userCard", value = "身份证号码", dataType = "String"),
		@ApiImplicitParam(paramType = "query", name = "accountType", value = "账号类型", dataType = "String"),
		@ApiImplicitParam(paramType = "query", name = "contact", value = "联系方式", dataType = "String"),
		@ApiImplicitParam(paramType = "query", name = "status", value = "状态", dataType = "String")
	})
	@PostMapping("/list")
//	@RequiresPermissions("sys:user:list")
	public RestResponse list(Integer currPage,Integer limit,String userName,String orgName,String orgType,String seal,String realName,String status,String contact,String roleCode,String userCard,String accountType) {

		Map<String, Object> params = new HashMap<String, Object>();

			params.put("page", PageUtil.getCurrPage(currPage));
			params.put("limit", PageUtil.getLimit(limit));
			params.put("userName", userName);
			params.put("realName", realName);
			params.put("userCard", userCard);
			params.put("accountType", accountType);
			params.put("contact", contact);
			params.put("status", status);
			params.put("orgType", orgType);
			params.put("orgName", orgName);
			params.put("roleCode", roleCode);
			params.put("seal", seal);
		Page page = sysUserService.queryPage(params);
		return RestResponse.success().put("page", page);
	}
	
	/**
	 * APP查询项目通讯录(项目成员列表)
	 * @param params
	 * @return RestResponse
	 */
	@ApiOperation(value = "APP查询项目通讯录(项目成员列表)", notes = "APP查询项目通讯录(项目成员列表)")
	@PostMapping("/list/contactsByApp")
	public CommonResult<Page<SysUserEntity>> list(@RequestBody @Validated ContactsByAppDto contactsByAppDto) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("page", PageUtil.getCurrPage(contactsByAppDto.getCurrPage()));
		params.put("limit", PageUtil.getLimit(contactsByAppDto.getLimit()));
		params.put("orgNo", contactsByAppDto.getPrjId());
		
		Page<SysUserEntity> page = sysUserService.queryPage(params);
		
		return CommonResult.success(page);
	}
	
	
	/**
	 * 企业查询本单位人员
	 */
	@ApiOperation(value = "企业查询本单位业务人员", notes = "企业查询本单位业务人员")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType = "query", name = "currPage", value = "当前页数", dataType = "Integer"),
		@ApiImplicitParam(paramType = "query", name = "limit", value = "分页步数", dataType = "Integer"),
		@ApiImplicitParam(paramType = "query", name = "userName", value = "登录名", dataType = "String"),
		@ApiImplicitParam(paramType = "query", name = "realName", value = "人员姓名", dataType = "String"),
		@ApiImplicitParam(paramType = "query", name = "userCard", value = "人员身份证号码", dataType = "String")
	})
	@PostMapping("/salesUserList")
	public RestResponse salesUserList(Integer currPage,Integer limit,String userName,String realName,String userCard) {
//		String orgNo = "";
//		SysUserEntity  user = this.getUser();
//		if(user != null && user.getUserId() != null){
//			SysUserEntity sysUserEntity = sysUserService.getById(user.getUserId());
//			if(sysUserEntity != null && SystemEnum.accountType.ORG.getKey().equals(sysUserEntity.getAccountType())){
//				Map<String, Object> params = new HashMap<String, Object>();
//				params.put("userId", sysUserEntity.getUserId());
//				List<SysOrgUserEntity> orgUserList = sysOrgUserService.queryAll(params);
//				if(orgUserList !=null && orgUserList.size()>0){
//					orgNo = orgUserList.get(0).getOrgNo();         //获取当前登陆企业账号所属单位id
//				}
//			}else{
//				return RestResponse.error("请使用企业账号查询");
//			}
//		}

		Map<String, Object> params = new HashMap<String, Object>();
			params.put("page", PageUtil.getCurrPage(currPage));
			params.put("limit", PageUtil.getLimit(limit));
			params.put("userName", userName);
			params.put("realName", realName);
			params.put("userCard", userCard);
			params.put("orgNo", getOrgNo());
			params.put("accountType", SystemEnum.accountType.USER.getKey());
//			params.put("noRoleCode", SystemEnum.roleType.ZZRY.getKey());
		
		Page page = sysUserService.queryPage(params);
		return RestResponse.success().put("page", page);
	}
	
	
	/**
	 * 企业查询本单位专家人员（主要用于分配专家）
	 */
	@ApiOperation(value = "企业查询本单位专家人员", notes = "企业查询本单位专家人员")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType = "query", name = "userName", value = "登录名", dataType = "String"),
		@ApiImplicitParam(paramType = "query", name = "realName", value = "人员姓名", dataType = "String"),
		@ApiImplicitParam(paramType = "query", name = "userCard", value = "人员身份证号码", dataType = "String")
	})
	@PostMapping("/expertUserList")
	public RestResponse expertUserList(String userName,String realName,String userCard) {
		 String orgNo = "";
		  SysUserEntity  user = this.getUser();
		if(user != null && user.getUserId() != null){
			SysUserEntity sysUserEntity = sysUserService.getById(user.getUserId());
			if(sysUserEntity != null && SystemEnum.accountType.ORG.getKey().equals(sysUserEntity.getAccountType())){
				Map<String, Object> params = new HashMap<String, Object>();
				params.put("userId", sysUserEntity.getUserId());
				List<SysOrgUserEntity> orgUserList = sysOrgUserService.queryAll(params);
				if(orgUserList !=null && orgUserList.size()>0){
					orgNo = orgUserList.get(0).getOrgNo();         //获取当前登陆企业账号所属单位id
				}
			}else{
				return RestResponse.error("请使用企业账号查询");
			}
		}

		Map<String, Object> params = new HashMap<String, Object>();
			params.put("userName", userName);
			params.put("realName", realName);
			params.put("userCard", userCard);
			params.put("orgNo", orgNo);
			params.put("accountType", SystemEnum.accountType.EXPERT.getKey());
			params.put("roleCode", "SCZJ");
			params.put("employStatus", "2");
		List<SysOrgUserEntity> list = sysOrgUserService.queryAll(params);
		return RestResponse.success().put("list", list);
	}
	
	/**
	 * 企业查询本单位人员(所有)
	 */
	@ApiOperation(value = "企业查询本单位人员(所有)", notes = "企业查询本单位人员(所有)")
	@ApiImplicitParams({
	@ApiImplicitParam(paramType = "query", name = "currPage", value = "当前页数", dataType = "Integer"),
	@ApiImplicitParam(paramType = "query", name = "limit", value = "分页步数", dataType = "Integer"),
	@ApiImplicitParam(paramType = "query", name = "userName", value = "登录名", dataType = "String"),
	@ApiImplicitParam(paramType = "query", name = "realName", value = "人员姓名", dataType = "String"),
	@ApiImplicitParam(paramType = "query", name = "userCard", value = "人员身份证号码", dataType = "String"),
	@ApiImplicitParam(paramType = "query", name = "accountType", value = "账号类型", dataType = "String"),
	@ApiImplicitParam(paramType = "query", name = "roleCode", value = "人员角色", dataType = "String")
	})
	@PostMapping("/myOrgUserList")
	public RestResponse myOrgUserList(Integer currPage,Integer limit,String userName,String realName,String userCard,String roleCode,String accountType) {
		 String orgNo = "";
		  SysUserEntity  user = this.getUser();
		  
		if(user != null && user.getUserId() != null){
			SysUserEntity sysUserEntity = sysUserService.getById(user.getUserId());
			if(sysUserEntity != null && SystemEnum.accountType.ORG.getKey().equals(sysUserEntity.getAccountType())){
				Map<String, Object> params = new HashMap<String, Object>();
				params.put("userId", sysUserEntity.getUserId());
				List<SysOrgUserEntity> orgUserList = sysOrgUserService.queryAll(params);
				if(orgUserList !=null && orgUserList.size()>0){
					orgNo = orgUserList.get(0).getOrgNo();         //获取当前登陆企业账号所属单位id
				}
			}else{
				return RestResponse.error("请登录企业账号查询!");
			}
		}else{
			return RestResponse.error("数据错误!");
		}

		Map<String, Object> params = new HashMap<String, Object>();
			params.put("page", PageUtil.getCurrPage(currPage));
			params.put("limit", PageUtil.getLimit(limit));
			params.put("userName", userName);
			params.put("realName", realName);
			params.put("userCard", userCard);
			params.put("orgNo", orgNo);
			params.put("accountType", accountType);
			params.put("roleCode", roleCode);
			
			//显示人员角色信息
			Page<SysOrgUserEntity> page = new Query<SysOrgUserEntity>(params).getPage();
			List<SysOrgUserEntity> orgUserList = sysOrgUserService.queryAll(params);
			if(orgUserList !=null && orgUserList.size()>0){
				for(SysOrgUserEntity orgUser:orgUserList){
					List<SysRoleEntity> roleList = sysRoleService.queryRoleList(orgUser.getUserId());
					orgUser.setRoleList(roleList);
				}
			}
			
			page.setRecords(orgUserList);

		return RestResponse.success().put("page", page);
	}

	/**
	 * 企业账号保存本单位人员
	 */
	@ApiOperation(value = "企业账号保存本单位人员", notes = "企业账号保存本单位人员")
	@PostMapping("/saveSalesUser")
	public RestResponse saveSalesUser(SysUserEntity user) {
//		 String orgNo = "";
//		 SysUserEntity  loginUser = this.getUser();
//		if(loginUser != null && loginUser.getUserId() != null){
//			SysUserEntity sysUserEntity = sysUserService.getById(loginUser.getUserId());
//			if(sysUserEntity != null && SystemEnum.accountType.ORG.getKey().equals(sysUserEntity.getAccountType())){
//				Map<String, Object> params = new HashMap<String, Object>();
//				params.put("userId", sysUserEntity.getUserId());
//				List<SysOrgUserEntity> orgUserList = sysOrgUserService.queryAll(params);
//				if(orgUserList !=null && orgUserList.size()>0){
//					orgNo = orgUserList.get(0).getOrgNo();    //获取当前登陆企业账号所属单位id
//				}
//			}else{
//				return RestResponse.error("请登录企业账号查询!");
//			}
//		}else{
//			return RestResponse.error("数据错误,获取登录人信息失败!");
//		}

		if(user !=null && StringUtils.isNotBlank(user.getUserName())){
        	SysUserEntity sysUserEntity = sysUserService.queryByUserName(user.getUserName());
        	if(sysUserEntity !=null){
        		return RestResponse.error("添加用户登录名不能重复");
        	}
        }
		
		Map<String, Object> params = new HashMap<String, Object>();
		user.setStatus(AptitudeEnum.aptitudeStatus.YBSYPZ.getKey());              //审核中状态（此处为启用状态）
		user.setAccountType(SystemEnum.accountType.USER.getKey());                //注册机构的人员账号类型默认为企业账号
		user.setCreateUserId(getUserId());
		user.setCreateUserOrgNo(getOrgNo());
		user.setOrgNo(getOrgNo());
		sysUserService.addUser(user, params);
		return RestResponse.success();
	}
	
	/**
	 * wangLc
	 * 企业账号删除本单位人员
	 */
	@ApiOperation(value = " 企业账号删除本单位人员", notes = " 企业账号删除本单位人员")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType = "query", name = "userId", value = "人员Id", dataType = "String")
	})
	@PostMapping("/deleteSalesUser")
	public RestResponse deleteSalesUser(String[] userIds) {
		
		if (ArrayUtils.contains(userIds, Constant.SUPER_ADMIN)) {
			return RestResponse.error("系统管理员不能删除");
		}
		if (ArrayUtils.contains(userIds, getUserId())) {
			return RestResponse.error("当前用户不能删除");
		}

		String orgNo = "";
		SysUserEntity  loginUser = this.getUser();
		if(loginUser != null && loginUser.getUserId() != null){
			SysUserEntity sysUserEntity = sysUserService.getById(loginUser.getUserId());
			if(sysUserEntity != null && SystemEnum.accountType.ORG.getKey().equals(sysUserEntity.getAccountType())){
				Map<String, Object> params = new HashMap<String, Object>();
				params.put("userId", sysUserEntity.getUserId());
				List<SysOrgUserEntity> orgUserList = sysOrgUserService.queryAll(params);
				if(orgUserList !=null && orgUserList.size()>0){
					orgNo = orgUserList.get(0).getOrgNo();     //获取当前登陆企业账号所属单位id
				}
			}else{
				return RestResponse.error("请登录企业账号查询!");
			}
		}else{
			return RestResponse.error("数据错误,获取登录人信息失败!");
		}
		sysUserService.deleteUsers(userIds,orgNo);

		 return RestResponse.success();
	}
	
	/**
	 * 获取当前登录的人员基本信息
	 * @return RestResponse
	 */
	@ApiOperation(value = "获取当前登录的人员基本信息-人员-资质-角色", notes = "获取登录的人员信息")
	@PostMapping("/userInfomation")
	public RestResponse userInfomation() {
		   SysUserEntity  user = this.getUser();
		  if (user != null && user.getUserId() != null) {
	            String userId = user.getUserId();
	            Map<String, Object> params = new HashMap<String, Object>();
		        params.put("userId", userId);
		        //获取当前登录人的人员资质信息
		        List<SysUserAptitudeEntity> userAptitudeList = sysUserAptitudeService.queryAll(params);
		        if(userAptitudeList != null && userAptitudeList.size()>0){
		        	user.setUserAptitudeList(userAptitudeList);
		        }
		        //获取当前登录人角色信息
		        List<SysRoleEntity> roleList = sysRoleService.queryRoleList(userId);
		        if(roleList != null && roleList.size()>0){
		        	user.setRoleList(roleList);
		        }

		        return RestResponse.success().put("user", user);
	        }else{
	        	return RestResponse.error("获取当前登陆人信息失败！");
	        }
	       
	}
	
	/**
	 * 获取登录资质人员用户信息
	 * @return RestResponse
	 */
	@ApiOperation(value = "获取登录的用户信息", notes = "获取登录的用户信息")
	@GetMapping("/info")
	public RestResponse info() {
		return RestResponse.success().put("user", getUser());
	}
	
	
	/**
	 * 获取登录资质人员用户信息
	 * @return RestResponse
	 */
	@ApiOperation(value = "获取登录的用户详细信息", notes = "获取登录的用户详细信息")
	@PostMapping("/getUserInfomation")
	public RestResponse getUserInfomation() {
	
		SysUserEntity sysUserEntity  = getUser();
		Map<String, Object> params = new HashMap<String, Object>();
			params.put("userId", sysUserEntity.getUserId());
			params.put("employStatus", "2");
		List<SysOrgUserEntity> list = sysOrgUserService.queryAll(params);
		List<String> orgNameList = new ArrayList<String>();
		List<String> sealList = new ArrayList<String>();
		
		if(list != null && list.size()>0){
			for(SysOrgUserEntity sysOrgUserEntity:list){
				orgNameList.add(sysOrgUserEntity.getOrgName());
				String name = sysDictService.getNameByValueAndGroupId(sysOrgUserEntity.getSeal(),"DUTY_TYPE");
				sealList.add(name);
			}
			sysUserEntity.setOrgNameList(orgNameList);
			sysUserEntity.setSealList(sealList);
		}
		return RestResponse.success().put("user", getUser());
	}
	
	/**
	 * 获取登录账号的基本信息
	 * @return RestResponse
	 */
	@ApiOperation(value = "获取登录账号的基本信息", notes = "获取登录的用户信息")
	@PostMapping("/baseInfomation")
	public RestResponse baseInfomation() {
		 SysUserEntity  loginUser = getUser();
		 if(loginUser != null){
			 String accountType = loginUser.getAccountType();
			 if(SystemEnum.accountType.ORG.getKey().equals(accountType)){
					if(loginUser != null && loginUser.getUserId() != null){
						SysUserEntity sysUserEntity = sysUserService.getById(loginUser.getUserId());
						if(sysUserEntity != null && SystemEnum.accountType.ORG.getKey().equals(sysUserEntity.getAccountType())){
							Map<String, Object> params = new HashMap<String, Object>();
							params.put("userId", sysUserEntity.getUserId());
							List<SysOrgUserEntity> orgUserList = sysOrgUserService.queryAll(params);
							SysOrgEntity  org =   sysOrgService.getById(orgUserList.get(0).getOrgNo());
							if(org != null){
								return RestResponse.success().put("baseMation", org);
							}
						}
					}
			 }else if(SystemEnum.accountType.USER.getKey().equals(accountType)){
				 return RestResponse.success().put("user", getUser());
			 }
		 }
		 return RestResponse.success();
	}

	/**
	 * 修改登录用户密码
	 *  ID
	 *  ORG_NO
	 *  REAL_NAME
	 *  USER_CARD
	 *  GENDER
	 *  BIRTHDAY
	 *  PROVINCE
	 *  CITY
	 *  CONTACT
	 *  EXAMINANT
	 *  SEAL_NO
	 *  PROFESSIONAL
	 *  TECHNICAL
	 *  GRADUATE_SCHOOL
	 *  GRADUATE_TIME
	 *  MAJOR_SUBJECT
	 *  EDUCATION
	 *  CREATE_USER_ID
	 *  CREATE_TIME
	 *  SEAL
	 * @param form
	 * @return RestResponse
	 */
	@ApiOperation(value = "修改密码", notes = "修改密码")
	@SysLog("修改密码")
	@PostMapping("/password")
	public RestResponse password(PasswordForm form) {
		AbstractAssert.isBlank(form.getNewPassword(), "新密码不为能空");
		// sha256加密
		String password = new Sha256Hash(form.getPassword(), getUser().getSalt()).toHex();
		// sha256加密
		String newPassword = new Sha256Hash(form.getNewPassword(), getUser().getSalt()).toHex();
		// 更新密码
		boolean flag = sysUserService.updatePassword(getUserId(), password, newPassword);
		if (!flag) {
			return RestResponse.error("原密码不正确");
		}
		return RestResponse.success();
	}

	/**
	 * 根据主键查询详情
	 * @param userId
	 * @return RestResponse
	 */
	@ApiOperation(value = "主键查询详情", notes = "主键查询详情")
	  @ApiImplicitParams({
     @ApiImplicitParam(paramType = "query", name = "userId", value = "人员id", dataType = "String")
     })
	@PostMapping("/info/{userId}")
//	@RequiresPermissions("sys:user:info")
	public RestResponse info(String userId) {
		SysUserEntity user = sysUserService.getById(userId);
		// 获取用户所属的角色列表
		List<String> roleIdList = sysUserRoleService.queryRoleIdList(userId);
		user.setRoleIdList(roleIdList);
		return RestResponse.success().put("user", user);
	}
	
	
	
	/**
	 * 根据主键查询详情
	 * @param userId
	 * @return RestResponse
	 */
	@ApiOperation(value = "系统日志列表", notes = "根据条件获取系统日志列表")
	  @ApiImplicitParams({
     @ApiImplicitParam(paramType = "query", name = "userId", value = "人员id", dataType = "String")
     })
	@PostMapping("/userInfo")
	public RestResponse userInfo(String userId) {
		SysUserEntity user = sysUserService.getById(userId);
		return RestResponse.success().put("user", user);
	}


	/**
	 * 保存用户
	 * @param user
	 * @return RestResponse
	 */
	@ApiOperation(value = "保存用户", notes = "保存用户")
	@SysLog("保存用户")
	@PostMapping("/save")
//	@RequiresPermissions("sys:user:save")
	public RestResponse save(SysUserEntity user) {

		ValidatorUtils.validateEntity(user, AddGroup.class);
		Map<String, Object> params = new HashMap<>(2);
		params.put("dataScope", getDataScope());
		user.setStatus(AptitudeEnum.aptitudeStatus.YBSYPZ.getKey());              //审核中状态（此处为启用状态）
		user.setAccountType(SystemEnum.accountType.USER.getKey());                //注册机构的人员账号类型默认为企业账号
		user.setCreateUserId(getUserId());
		user.setCreateUserOrgNo(getOrgNo());
		List<String> roleIdList = user.getRoleIdList();
		if(roleIdList == null || roleIdList.size()<=0){
			return RestResponse.error("请添加人员角色");
		}
		
		if(user !=null && StringUtils.isNotBlank(user.getUserName())){
        	SysUserEntity sysUserEntity = sysUserService.queryByUserName(user.getUserName());
        	if(sysUserEntity !=null){
        		return RestResponse.error("用户登录名不能重复");
        	}
        }
		
		sysUserService.add(user, params);
		return RestResponse.success();
	}
	

	 /**
	 * 修改用户
	 * @param user
	 * @return RestResponse
	 */
	@ApiOperation(value = "修改用户", notes = "修改用户")
	@SysLog("修改用户")
	@PostMapping("/update")
	//@RequiresPermissions("sys:user:update")
	public RestResponse update(SysUserEntity user) {
		ValidatorUtils.validateEntity(user, UpdateGroup.class);

		Map<String, Object> params = new HashMap<>(2);
		params.put("dataScope", getDataScope());
		user.setCreateUserId(getUserId());
		user.setCreateUserOrgNo(getOrgNo());
		sysUserService.update(user, params);

		return RestResponse.success();
	}
	
	
	/**
	 * 修改用户
	 * @param user
	 * @return RestResponse
	 */
	@ApiOperation(value = "修改用户", notes = "修改用户")
	@SysLog("修改用户")
	@PostMapping("/updateByPerson")
	public RestResponse updateByPam(SysUserEntity user) {
		
		if(StringUtils.isBlank(user.getUserId())){
			return RestResponse.error("人员userId不能为空");
		}
		
		String contact = user.getContact();
		String email = user.getEmail();
		if(StringUtils.isBlank(contact)){
			return RestResponse.error("人员联系方式不能为空");
		}
		if(StringUtils.isBlank(email)){
			return RestResponse.error("人员邮箱不能为空");
		}
		
		SysUserEntity userEntity = sysUserService.getById(user.getUserId());
	
		if(userEntity == null){
			return RestResponse.error("无此人员信息");
		}
			userEntity.setContact(contact);
			userEntity.setEmail(email);
		Map<String, Object> paramsPam = new HashMap<>(2);
		paramsPam.put("noUpdate", "noUpdate");
		user.setCreateUserId(getUserId());
		user.setCreateUserOrgNo(getOrgNo());
		sysUserService.update(userEntity, paramsPam);

		return RestResponse.success();
	}

	/**
	 * 1、删除用户
	 * @param userIds
	 * @return RestResponse
	 */
	@ApiOperation(value = "删除用户", notes = "根据userId删除用户（支持多id删除）")
	@SysLog("删除用户")
	@PostMapping("/delete")
	//@RequiresPermissions("sys:user:delete")
	public RestResponse delete(String[] userIds) {
		if (ArrayUtils.contains(userIds, Constant.SUPER_ADMIN)) {
			return RestResponse.error("系统管理员不能删除");
		}
		if (ArrayUtils.contains(userIds, getUserId())) {
			return RestResponse.error("当前用户不能删除");
		}

		sysUserService.deleteBatch(userIds);
		return RestResponse.success();
	}

	/**
	 * 批量重置密码
	 * @return RestResponse
	 */
	@ApiOperation(value = "批量重置密码", notes = "重置密码")
	@SysLog("批量重置密码")
	@PostMapping("/resetPws")
//	@RequiresPermissions("sys:user:resetPws")
	public RestResponse resetPws(String[] userIds) {
		if (ArrayUtils.contains(userIds, Constant.SUPER_ADMIN)) {
			return RestResponse.error("系统管理员不能重置");
		}

		if (ArrayUtils.contains(userIds, getUserId())) {
			return RestResponse.error("当前用户不能重置");
		}
		sysUserService.resetPws(userIds);
		return RestResponse.success();
	}

	/**
	 * 重置密码
	 * @return RestResponse
	 */
	@ApiOperation(value = "重置密码", notes = "重置密码")
	@SysLog("重置密码")
	@PostMapping("/resetPw")
	//@RequiresPermissions("sys:user:resetPw")
	public RestResponse resetPw(String userId) {
		if (Constant.SUPER_ADMIN.equals(userId)) {
			return RestResponse.error("系统管理员不能重置");
		}

		if (getUserId().equals(userId)) {
			return RestResponse.error("当前用户不能重置");
		}
		
		if (userId == null || "".equals(userId)) {
			return RestResponse.error("当前用户不能重置");
		}
		sysUserService.resetPw(userId);
		return RestResponse.success();
	}
	
	/**
	 * 用户启用/禁用切换
	 *
	 * @param userId
	 * @return RestResponse
	 */
	@ApiOperation(value = "角色启用/禁用切换", notes = "角色启用/禁用切换")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType = "query", name = "userId", value = "用户id", dataType = "String")
    })
	@SysLog("角色启用/禁用切换")
	@PostMapping("/updateUseStatus")
	//@RequiresPermissions("sys:role:update")
	public RestResponse updateUseStatus(String userId, String status) {
		SysUserEntity sysUserEntity = sysUserService.getById(userId);
		if(null==sysUserEntity) {
			return RestResponse.error("用户未找到");
		}
		if("1".equals(status)) {
			sysUserEntity.setStatus("2");
		}else {
			sysUserEntity.setStatus("3");
		}
		sysUserService.updateById(sysUserEntity);
		
		return RestResponse.success();
	}
	
	/**
	 * 获取当前登录用户信息
	 * @return
	 */
	@ApiOperation(value = "获取当前登录用户信息", notes = "获取当前登录用户信息")
	@PostMapping("/currentUser")
	public RestResponse currentUserInfo() {
		return RestResponse.success().put("user", this.getUser()).put("org", this.getOrg());
	}
	
}
